package com.yzy.dao;



import com.yzy.entity.Patients;
import com.yzy.utils.Utils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PatientDao {
    private Utils util = new Utils();
    //登陆验证
    public  int longin(String pphone,String ppass){
        String sql = "select count(*)from patient where Pphone=? and Ppass=?";
        PreparedStatement ps=util.createStatement(sql);
        ResultSet rs=null;
        int result = 0;
        try {
            ps.setString(1,pphone);
            ps.setString(2,ppass);
            rs=ps.executeQuery();
            while (rs.next()){
                result=rs.getInt("count(*)");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }return result;
    }

    //zc
    public  int add(Patients patient){
        String sql="insert into patient(pphone,Pname,Page,Ppass,Psex,history)"+"values(?,?,?,?,?,?)";
        PreparedStatement ps =util.createStatement(sql);
        int result =0;
        try {
            ps.setString(1,patient.getPphone());
            ps.setString(2,patient.getPname());
            ps.setString(3,patient.getPage());
            ps.setString(4,patient.getPpass());
            ps.setString(5,patient.getPsex());
            ps.setString(6,patient.getHistory());

            result =ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close();
        }
        return result;
    }

    //chaxun
    public List findAll(){
        String sql = "select * from patient";
        PreparedStatement ps = util.createStatement(sql);
        ResultSet rs = null;
        List patientList = new ArrayList();
        try {
            rs = ps.executeQuery();

            while (rs.next()){
                String pphone=rs.getString("pphone");
                String pname=rs.getString("pname");
                String page=rs.getString("page");

                String ppass=rs.getString("ppass");
                String psex=rs.getString("psex");
                String history=rs.getString("history");
                Patients patients =new Patients(pphone,pname,page,ppass,psex,history);
                patientList.add(patients);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }
        return patientList;
    }


}
